package com.qianli.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianli.service.impl.ImageServiceImpl;
import com.qianli.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.qianli.service.impl"})
public class imagesController {

    @Autowired
    ImageServiceImpl imageService;

    @RequestMapping(value = "/upload")
    public Object supplementData(@RequestParam(value = "file") MultipartFile[] file, String imageType,
                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map map = new HashMap();
        String uploadPath = request.getSession().getServletContext().getRealPath("/upload");
        System.out.printf("imageType="+imageType);
        for (MultipartFile fe : file) {
            Integer a = (int) (Math.random() * 900000 + 100000);
            String s = a.toString();
            fe.getName();
            String fileName = fe.getOriginalFilename();
            StringBuffer sb = new StringBuffer(fileName);
            String fileNamet = (sb.insert(0, s)).toString();
            StringBuffer sh = new StringBuffer(fileNamet);
            String y = "http://localhost/upload/";
            String fileNames = (sh.insert(0, y)).toString();
            Map maps = new HashMap();
            maps.put("imageType",imageType);
            maps.put("imageUrl",fileNames);
            maps.put("imageTime", DataUtil.getCurrent());
            imageService.insertImage(maps);
            //Long size = fe.getSize();
            String contentType = fe.getContentType();
            try {
                fe.transferTo(new File(uploadPath, fileNamet));
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        map.put("data", "success");// fail
        return map;
    }

    @RequestMapping("/deleteImage")
    public Object deleteImage(@RequestParam Map map){
        imageService.deleteImage(map);
        HashMap m=new HashMap();
        m.put("data","success");
        return m;
    }


    @RequestMapping(value = "/selectImages")
    public  Object selectImages(@RequestParam Map map){
        int page=1;
        if (map.get("page")!=null){
            page=Integer.parseInt(map.get("page").toString());
        }
        PageHelper.startPage(page,2);
        List<Map> list=imageService.selectImages(map);
        //3、获取分页查询后的数据
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        HashMap m=new HashMap();
        m.put("pa",pageInfo.getPages());
        m.put("datas",list);
        m.put("number",pageInfo.getTotal());
        return m;
    }

}
